package com.paipai.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.paipai.entity.RequeStats;
import com.paipai.entity.User;
import com.paipai.jwtproperty.JwtProperties;
import com.paipai.service.AuthService;
import com.paipai.utils.CookieUtils;
import com.paipai.utils.JwtUtils;
import com.paipai.utils.UserInfo;

@RestController
public class AuthController {
	@Autowired
	private AuthService authService;
	
	
	@Autowired
	private JwtProperties jwp;

	/**
	 * 登录
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/accredit")
	public Object authentication(@RequestBody User user,HttpServletRequest request,
            HttpServletResponse response) {
		//查询生成token
		String token =authService.authentication(user);
		
		
		RequeStats stats = new RequeStats();
		
		//登录失败
		if(token==null||"".equals(token)) {
			stats.setStats("no");
			return stats;
		}
		// 将token写入cookie,并指定httpOnly为true，防止通过JS获取和修改
        CookieUtils.setCookie(request, response, "loginToken",token, 36000, null);
       String requestURL = request.getRequestURL().toString();
       System.out.println("url:"+ requestURL);
		//System.out.println(token);
      
       
       stats.setStats("yes");
       stats.setToken(token);
		return stats;

	}
	
	
	/**
	 * 验证是否登录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/verify")
	public Object verify(HttpServletRequest request,
            HttpServletResponse response) {
		
		try {
			String loginToken = CookieUtils.getCookieValue(request, "loginToken");
			
			UserInfo userInfo = JwtUtils.getInfoFromToken(loginToken, jwp.getPublicKey());

			// 重新生成token
			String token = JwtUtils.generateToken(userInfo, jwp.getPrivateKey(), jwp.getExpire());
			// 重新放入cookie
			CookieUtils.setCookie(request, response, "loginToken", token, 36000, null);
			return userInfo;
		} catch (Exception e) {
			System.out.println("token解析错误");
			// e.printStackTrace();
		}
		return "no";

	}
	/**
	 * 注销
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response) {
		
		
		Cookie cookie2 = new Cookie("loginToken",null);

		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {

			if (cookie.getName().equals("loginToken")) {
				//时间改为零
				cookie.setMaxAge(0);
				cookie.setValue("");
				cookie.setPath("/"); // 路径一定要写上，不然销毁不了
				response.addCookie(cookie);
				System.out.println("CC:"+cookie.getValue());
				return "ok";
			}
		}
		return "no";

	}

}
